/*
 * Test program for 8x8 LED matrix (single color) and two shift registor
 * test No.3
 *
 * HISTORY
 * 2010/8/23  New
 *
 */

/*
 * Copyright (c) 2010 elekid
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

// for 7 segments x 4 display

int CLOCK_PIN =2;
int DATA_PIN  =3;

byte PATTERN[16][8] = {
{
B11101111, 
B11011111, 
B10111111, 
B01111111, 
B01111111, 
B10111111, 
B11011111, 
B11101111
},
{
B11011111, 
B10111111, 
B01111111, 
B01111111, 
B10111111, 
B11011111, 
B11101111, 
B11110111
},
{
B10111111, 
B01111111, 
B01111111, 
B10111111, 
B11011111, 
B11101111, 
B11110111, 
B11111011
},
{
B01111111, 
B01111111, 
B10111111, 
B11011111, 
B11101111, 
B11110111, 
B11111011, 
B11111101
},
{
B01111111, 
B10111111, 
B11011111, 
B11101111, 
B11110111, 
B11111011, 
B11111101, 
B11111110
},
{
B10111111, 
B11011111, 
B11101111, 
B11110111, 
B11111011, 
B11111101, 
B11111110, 
B11111110
},
{
B11011111, 
B11101111, 
B11110111, 
B11111011, 
B11111101, 
B11111110, 
B11111110, 
B11111101
},
{
B11101111, 
B11110111, 
B11111011, 
B11111101, 
B11111110, 
B11111110, 
B11111101, 
B11111011
},
{
B11110111, 
B11111011, 
B11111101, 
B11111110, 
B11111110, 
B11111101, 
B11111011, 
B11110111
},
{
B11111011, 
B11111101, 
B11111110, 
B11111110, 
B11111101, 
B11111011, 
B11110111, 
B11101111
},
{
B11111101, 
B11111110, 
B11111110, 
B11111101, 
B11111011, 
B11110111, 
B11101111, 
B11011111
},
{
B11111110, 
B11111110, 
B11111101, 
B11111011, 
B11110111, 
B11101111, 
B11011111, 
B10111111
},
{
B11111110, 
B11111101, 
B11111011, 
B11110111, 
B11101111, 
B11011111, 
B10111111, 
B01111111
},
{
B11111101, 
B11111011, 
B11110111, 
B11101111, 
B11011111, 
B10111111, 
B01111111, 
B01111111
},
{
B11111011, 
B11110111, 
B11101111, 
B11011111, 
B10111111, 
B01111111, 
B01111111, 
B10111111
},
{
B11110111, 
B11101111, 
B11011111, 
B10111111, 
B01111111, 
B01111111, 
B10111111, 
B11011111
}
};

#define WAITS 1
#define LOOPMAX 2


void setup()
{

  pinMode(CLOCK_PIN, OUTPUT);
  pinMode(DATA_PIN,  OUTPUT);

}
void loop()
{ 
  int buffer[8];

  /*
  for ( int i=0 ; i<8 ; i++) {
    buffer[i]= PATTERN[i];
  }
  */

  for (int step=0 ; step < 16 ; step++) {
    // 
    for ( int disp_cnt = 0 ; disp_cnt < LOOPMAX ; disp_cnt++) {
      for (int row=0 ; row < 8 ; row++) {
        byte b = PATTERN[step][row];
        shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, b);
        if ( row == 0 ) {
          b = 1;
        } else {
          b = 1 << row;
        }
        shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, b);
        delay(2);
        shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 0xFF);
        shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, 0xFF);
        delay(1);
      } // row
    } // disp_cnt
  } // step
}    

void matrix_clear(){
  // not implemented.
}

void matrix_on(int i,int j) {
  byte b=0xFF ^ 1 << j;
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, b);
  if ( i == 0) {
    b = 1 ;
  } else {
    b = 1 << i;
  }
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, b);
  
}
